package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.result.CustomizeException;
import com.itheima.reggie.result.R;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;
    /*
    * 添加套餐
    * */
    @Override
    @Transactional
    public void addSetmealDish(SetmealDto setmealDto) {
        //添加套餐的基本信息
        this.save(setmealDto);

        Long setmealId = setmealDto.getId();

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (int i = 0; i < setmealDishes.size(); i++) {
            setmealDishes.get(i).setSetmealId(setmealId);
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /*
     * 分页查询
     * */
    @Override
    public R<Page> selectSetmealDish(int page, int pageSize, String name) {
        Page<SetmealDto> pageInfo = new Page<>(page, pageSize);

        List<SetmealDto> setmealDtos = setmealMapper.selectSetmealDish(pageInfo, name);

        pageInfo.setRecords(setmealDtos);

        return R.success(pageInfo);
    }

    /*
    * 根据id查询套餐信息
    * */
    @Override
    public SetmealDto selectById(Long id) {
        //获取套餐的基本信息
        Setmeal setmeal = this.getById(id);
        //拷贝到套餐+中
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //设置套餐+中的菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getDishId, id);
        List<SetmealDish> list = setmealDishService.list(lqw);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /*
    * 修改套餐信息
    * */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDto setmealDto) {
        //更新基本信息
        this.updateById(setmealDto);

        //删除之前的菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(lqw);

        //添加新的信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (int i = 0; i < setmealDishes.size(); i++) {
            setmealDishes.get(i).setSetmealId(setmealDto.getId());
        }

        setmealDishService.saveBatch(setmealDishes);
    }

    /*
    * 删除套餐信息
    * */
    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        //查询套餐的状态是否可以删除
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1);

        int count = this.count(lqw);

        if (count > 0) {
            throw new CustomizeException("套餐包含起售状态，不能删除");
        }

        //删除套餐基本信息
        this.removeByIds(ids);
        //删除套餐中的菜品信息
        LambdaQueryWrapper<SetmealDish> lqwDish = new LambdaQueryWrapper<>();
        lqwDish.in(SetmealDish::getSetmealId, ids);

        setmealDishService.remove(lqwDish);
    }

    /*
    * 售卖状态修改
    * */
    @Override
    public void status(Integer status, Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            Long id = ids[i];
            //根据id得到每个套餐
            Setmeal setmeal = this.getById(id);
            setmeal.setStatus(status);

            this.updateById(setmeal);
        }
    }


}
